/*
 * Copyright (c) 2011 Junichiro Okuno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Junichiro Okuno.
 */

package tcp_net.management.user_account;

import tcp_net.management.user_account.debug.DebugInfo;
import tcp_net.management.FileManager;
import java.util.List;
import java.util.ArrayList;

/**
	This class manages a list of email user accounts
	@author J. Okuno
*/
public class UserAccountManager
{
	/**
		Debug information
	*/
	private final static boolean DEBUG = DebugInfo.USER_ACCOUNT_MANAGER_DEBUG;
	
	/**
		A list of user accounts
	*/
	private ArrayList<UserAccount> users;
	
	/**
		A file manager
	*/
	private FileManager file_man;
	
	/**
		Init UserAccountManager
	*/
	public UserAccountManager() 
	{
		users = new ArrayList<UserAccount>();
		file_man = new FileManager();
		
		DebugInfo.print(DEBUG,"UserAccountManager created");
	}
	
	/**
		This method provides a list of user accounts
		@return a list of user accounts
	*/
	public List<UserAccount> getUsers() { return users; }
	
	/**
		This method add a user account to the list
		@param name a user account name
		@param pass a password
		@return true if a user account is added to the list
	*/
	public boolean addUser(String name, String pass)
	{
		for(UserAccount usr : users)
		{
			if(usr.getUserName().equals(name)) 
			{ 
				DebugInfo.print(DEBUG,"ERROR User name: "+name+" already added.");
				return false; 
			}
		}
		
		users.add(new UserAccount(name, pass));
		
		DebugInfo.print(DEBUG,"User name: "+name+" added.");
		return true;
	}
	
	/**
		This method adds a new user account to the list
		@param ua a new user account
		@return true if a new user account is added to the list
	*/
	public boolean addUser(UserAccount ua)
	{
		for(UserAccount usr : users)
		{
			if(usr.getUserName().equals(ua.getUserName())) 
			{ 
				DebugInfo.print(DEBUG,"ERROR User name: "+ua.getUserName()+" already added.");
				return false; 
			}
		}
		
		users.add(ua);
		
		DebugInfo.print(DEBUG,"User name: "+ ua.getUserName() +" added.");
		return true;
	}
	
	/**
		This method changes a user's password
		@param user_name a user account name
		@param pass a password of a user
		@return true if a user's password is changed
	*/
	public boolean changeUserPass(String user_name, String pass)
	{
		for(UserAccount usr : users)
		{
			if(usr.getUserName().equals(user_name)) 
			{ 
				DebugInfo.print(DEBUG,"User name: "+ user_name +" password changed.");
				usr.setPassword(pass);
				return true; 
			}
		}
		DebugInfo.print(DEBUG,"ERROR User name: "+ user_name +" not in the list.");
		
		return false;
	}
	
	/**
		This method removes a user account by name
		@param name a user account by name
		@return true if a user account is removed
	*/
	public boolean removeUser(String name)
	{
		//Remove a mailbox of this new user
		for(UserAccount usr : users)
		{
			if((usr.getUserName().equals(name)))
			{
				users.remove(usr);
				DebugInfo.print(DEBUG,"name: "+name+" removed.");
				return true;
			}
		}
		
		DebugInfo.print(DEBUG,"name: "+name+" NOT removed.");
		return false;
	}
	
	/**
		This method removes all users in a list
	*/
	public void removeAllUsers()
	{
		users.clear();
		
		/*
		for(UserAccount u : users) 
		{ 
			removeUser(u.getUserName()); 
		}*/
		
		DebugInfo.print(DEBUG,"All users deleted");	
	}
	
	/**
		This method enable all user in a list
	*/
	public void enableAllUsers()
	{
		for(UserAccount u : users) { u.enable(); }
		DebugInfo.print(DEBUG,"All users enabled");
	}
	
	/**
		This method disable all users in a list
	*/
	public void disableAllUsers()
	{
		for(UserAccount u : users) { u.disable(); }
		DebugInfo.print(DEBUG,"All users disabled");
	}
	
	/**
		This method provides a user account object by user name (Returns a first macthed user)
		@param name a user name
		@return a user account object by user name
	*/
	public UserAccount getUserAccount(String name)
	{
		DebugInfo.print(DEBUG,"UserAccount name: "+name+" requested.");
		DebugInfo.print(DEBUG,"No. of users listed: "+users.size());
						
		for(UserAccount u : users) 
		{ 
			if(u.getUserName().equals(name)) 
			{ 
				DebugInfo.print(DEBUG,"User "+name+" is found");
				return u; 
			}
		}
		
		DebugInfo.print(DEBUG,"User "+name+" is not found");
		return null;
	}
}